package com.yx.shop.dao.impl;

import com.yx.shop.dao.CartDao;
import com.yx.shop.entitly.Cart;
import com.yx.shop.entitly.Product;
import com.yx.shop.utils.JDBCUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname CartDaoImpl
 * @Description TODO:
 * @Date 2021/05/07 23:11
 * @Created by yx
 */
public class CartDaoImpl implements CartDao {
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
    @Override
    public Cart hasCart(Integer uid, Integer pid) {
        String sql = "SELECT cid,uid,cart.pid AS pid,ccount,cnum,pname,ptime,pimage,pprice,pstate,pinfo FROM cart INNER JOIN product ON cart.pid = product.pid WHERE uid=? AND cart.pid = ?;";
        try {
            Map<String, Object> query = queryRunner.query(sql, new MapHandler(), uid, pid);
            if (query!=null){
                Cart cart = new Cart();
                Product product = new Product();
                BeanUtils.populate(cart,query);
                BeanUtils.populate(product,query);
                cart.setProduct(product);
                return cart;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateCart(Cart cart) {
        String sql = "update cart set cnum=?,ccount=? where uid=? and pid= ?;";
        try {
            return queryRunner.update(sql, cart.getCnum(), cart.getCcount(), cart.getUid(), cart.getPid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void insertCart(Cart cart) {
        String sql = "insert into cart(uid,pid,cnum,ccount) values(?,?,?,?);";
        try {
            queryRunner.update(sql,cart.getUid(),cart.getPid(),cart.getCnum(),cart.getCcount());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Cart> getAllCartByUid(Integer uid){
        //根据uid查询该用户所有的购物车
        try {
            String sql = "SELECT cid,uid,cart.pid AS pid,ccount,cnum,pname,ptime,pimage,pprice,pstate,pinfo FROM cart INNER JOIN product ON cart.pid = product.pid WHERE uid=?;";
            List<Map<String, Object>> query = queryRunner.query(sql, new MapListHandler(), uid);
            if (query!=null){
                List<Cart> cartList = new ArrayList<>();
                for (Map<String, Object> stringObjectMap : query) {
                    //封装数据
                    Cart cart = new Cart();
                    Product product = new Product();
                    BeanUtils.populate(cart,stringObjectMap);
                    BeanUtils.populate(product,stringObjectMap);
                    cart.setProduct(product);
                    //将cart添加到集合
                    cartList.add(cart);
                }
                return cartList;
            }
        } catch (SQLException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int clearCart(int uid) {
        try {
            String sql = "delete from cart where uid=?;";
            return queryRunner.update(sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteCart(Integer pid, int uid) {
        try {
            String sql = "delete from cart where uid = ? and pid = ?;";
            return queryRunner.update(sql,uid,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
